package com.gin.no_gay_alliance.viewmodel;

import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;

import java.util.Locale;

/**
 * 滚动目标
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/9 10:36
 **/
public class ScrollTarget implements Parcelable {
    public static final Creator<ScrollTarget> CREATOR = new Creator<ScrollTarget>() {
        @Override
        public ScrollTarget createFromParcel(Parcel in) {
            return new ScrollTarget(in);
        }

        @Override
        public ScrollTarget[] newArray(int size) {
            return new ScrollTarget[size];
        }
    };
    /**
     * 目标页数
     */
    public int page;
    /**
     * 目标楼层，与回复id二选一
     */
    public Integer floorNumber;
    /**
     * 目标回复id，与楼层二选一
     */
    public Long replyId;

    public ScrollTarget() {
    }

    public ScrollTarget(int floorNumber) {
        this.floorNumber = floorNumber;
        this.page = floorNumber / 20 + 1;
    }

    public ScrollTarget(int page, long replyId) {
        this.page = page;
        this.replyId = replyId;
    }

    protected ScrollTarget(Parcel in) {
        page = in.readInt();
        if (in.readByte() == 0) {
            floorNumber = null;
        } else {
            floorNumber = in.readInt();
        }
        if (in.readByte() == 0) {
            replyId = null;
        } else {
            replyId = in.readLong();
        }
    }

    @NonNull
    @Override
    public String toString() {
        if (floorNumber != null) {
            return String.format(Locale.CHINA, "滚动目标: 第 %d 页 %d 楼", page, floorNumber);
        }
        return String.format(Locale.CHINA, "滚动目标: 第 %d 页 回复 %d", page, replyId);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(page);
        if (floorNumber == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(floorNumber);
        }
        if (replyId == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(replyId);
        }
    }
}
